/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.sensor.core;

import java.util.ServiceLoader;
import nl.cloudfarming.sensor.Receiver;
import nl.cloudfarming.sensor.ReceiverAlreadyRegisteredException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Controller for starting and stopping all Receivers available to the ServiceLoader.
 * 
 * @author Timon Veenstra
 */
public class ReceiverController {
        
        private static Log log = LogFactory.getLog(ReceiverController.class);

	public void startReceivers(){
            ServiceLoader<Receiver> receiverLoader = ServiceLoader.load(Receiver.class);

            for(Receiver receiver:receiverLoader){
                try {
                    receiver.start();
                } catch (ReceiverAlreadyRegisteredException ex) {
                    log.error("Error while starting Receivers." ,ex);
                }
            }
	}

	public void stopReceivers(){
		ServiceLoader<Receiver> receiverLoader = ServiceLoader.load(Receiver.class);

		for(Receiver receiver:receiverLoader){
			receiver.stop();
		}
	}
}
